package com.example.student.controller;

import com.example.student.domain.Teacher;
import com.example.student.service.TeacherService;
import com.example.student.util.AjaxResult;
import com.example.student.util.ResponseBean;
import com.example.student.util.SnGenerateUtil;
import com.example.student.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    TeacherService teacherService;

    @RequestMapping("/getTeacherList")
    @ResponseBody
    public ResponseBean getTeacherList() {
        ResponseBean bean = new ResponseBean();
        bean =  teacherService.getTeacherList();
        if (bean.getCount()> 0) {
            bean.setCode(200);
//            bean.setData(list);
        }
        return bean;
    }

    @RequestMapping("/addTeacher")
    @ResponseBody
    public ResponseBean addTeacher(@RequestBody Teacher teacher) {
        ResponseBean bean = new ResponseBean();
//        teacher.setSn(SnGenerateUtil.generateSn(teacher.getClazzId()));

        try {
            int count = teacherService.addTeacher(teacher);
            if (count > 0) {
                bean.setCode(200);
                bean.setMessage("保存成功");
            }
            else {
                bean.setCode(2001);
                bean.setMessage("保存失败");
            }
        } catch (Exception e) {
            e.printStackTrace();;
            bean.setCode(2001);
            bean.setMessage("保存失败");
        }

        return bean;
    }

    @RequestMapping("/deleteTeacher")
    @ResponseBody
    public ResponseBean deleteTeacher(@RequestParam(value = "idList",required=false) List<Long> idList) {
        ResponseBean bean = new ResponseBean();
        boolean count = teacherService.deleteTeacher(idList);
        if (count ) {
            bean.setCode(200);
            bean.setMessage("删除成功！");
        }
        else {
            bean.setCode(2001);
            bean.setMessage("删除失败！");
        }
        return bean;
    }


    @RequestMapping("/updateTeacher")
    @ResponseBody
    public ResponseBean updateTeacher(@RequestBody Teacher teacher) {
        ResponseBean bean = new ResponseBean();


        boolean isSuccess = teacherService.updateTeacher(teacher);

        if (isSuccess) {
            bean.setCode(200);
            bean.setMessage("修改成功！");
        }
        else {
            bean.setCode(2001);
            bean.setMessage("修改失败！");
        }
        return  bean;
    }
}
